package com.easy.work.function.main.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IntegerRes;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.base.syslib.util.MessageTools;
import com.base.syslib.util.TextUtil;
import com.easy.work.R;
import com.easy.work.app.AppSession;
import com.easy.work.app.BaseActivity;
import com.easy.work.app.BaseFragment;
import com.easy.work.function.main.SignUpSuccessAct;
import com.easy.work.http.BaseResponse;
import com.easy.work.http.HttpClient;
import com.easy.work.http.request.WorkRecommendRequest;
import com.easy.work.type.DeliveryType;
import com.easy.work.utils.StringUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by admin on 2017/12/19.
 * 推荐
 */
public class InputRecommendFragment extends BaseActivity implements View.OnTouchListener {
    @BindView(R.id.img_delete)
    ImageView imgDelete;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_card_no)
    EditText etCardNo;
    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.btn_upload)
    Button btnUpload;

    private String workId;

    public static void start(Context context, String workId) {
        Intent intent = new Intent(context, InputRecommendFragment.class);
        intent.putExtra("workId", workId);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frag_input_recommend);
        ButterKnife.bind(this);
        workId = getIntent().getStringExtra("workId");
    }


    @OnClick({R.id.img_delete, R.id.btn_upload})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_delete:
                finish();
                break;
            case R.id.btn_upload:
                workRecommend();
                break;
        }
    }

    /**
     * 我要推荐
     */
    private void workRecommend() {
        String name = etName.getText().toString().trim();
        if (StringUtil.isEmpty(name)) {
            MessageTools.showToast(getApplicationContext(), "请输入被推荐人姓名");
            return;
        }

        String idCard = etCardNo.getText().toString().trim();
        if (StringUtil.isEmpty(idCard)) {
            MessageTools.showToast(getApplicationContext(), "请输入被推荐人身份证号码");
            return;
        }
        if (!TextUtil.isIDCard(idCard)) {
            MessageTools.showToast(getApplicationContext(), "请输入正确的身份证号码");
            return;
        }
        String phoneNo = etMobile.getText().toString().trim();
        if (StringUtil.isEmpty(phoneNo)) {
            MessageTools.showToast(getApplicationContext(), "请输入被推荐人手机号码");
            return;
        }
        if (!TextUtil.isMobile(phoneNo)) {
            MessageTools.showToast(getApplicationContext(), "请输入正确的手机号码");
            return;
        }
        WorkRecommendRequest request = new WorkRecommendRequest();
        request.userId = AppSession.getInstance().getUserId();
        request.workId = workId;
        request.name = name;
        request.idCard = idCard;
        request.phone = phoneNo;
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                SignUpSuccessAct.start(oThis, DeliveryType.Recommend);
            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                MessageTools.showToast(oThis, msg);
            }
        });
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return true;
    }
}
